#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->lineEdit->setPlaceholderText("请输入新增的列名");

    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);

    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);

    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("年龄"));

    ui->tableWidget->setItem(0,0,new QTableWidgetItem("1001"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("小王"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("18"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("1002"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("19"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_3_clicked()
{
    int rowcnt = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(rowcnt);
}

void Widget::on_pushButton_clicked()
{
    int currow = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(currow);
}



void Widget::on_pushButton_4_clicked()
{
    int colcnt = ui->tableWidget->columnCount();
    const QString& newColName = ui->lineEdit->text();

    ui->tableWidget->insertColumn(colcnt);
    ui->tableWidget->setHorizontalHeaderItem(colcnt, new QTableWidgetItem(newColName));
}

void Widget::on_pushButton_2_clicked()
{
    int curcol = ui->tableWidget->currentColumn();
    ui->tableWidget->removeColumn(curcol);
}
